﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ASPItalia.ModelVirtualCasting.Web;
using ASPItalia.ModelVirtualCasting.Web.Controllers;
using ASPItalia.ModelVirtualCasting.Web.Models;
using Rhino.Mocks;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.Web.Tests.Controllers
{
    [TestClass]
    public class HomeControllerTest
    {
        #region initialization
        MockRepository mocks;
        IModelRepository modelRepository;
        INewsRepository newsRepository;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();

            // Mock di ModelRepository
            modelRepository = mocks.DynamicMock<IModelRepository>(null);
            modelRepository
                .Expect(m => m.GetRandomModels(5))
                .IgnoreArguments()
                .Return(new List<ModelDTO>().AsQueryable());

            // Mock di INewsRepository
            newsRepository = mocks.DynamicMock<INewsRepository>(null);
            newsRepository
                .Expect(n => n.GetLastNewsDTO(5))
                .IgnoreArguments()
                .Return(new List<NewsDTO>());
        }
        #endregion
        
        private HomeController GetController()
        {
            return new HomeController(modelRepository, newsRepository);
        }
        
        [TestMethod]
        public void Index_ReturnsView_WithModel()
        {
            // Arrange
            HomeController controller = this.GetController();
            mocks.ReplayAll();

            // Act
            var res = controller.Index() as ViewResult;

            // Assert
            Assert.IsNotNull(res, "Index should return a view");
            Assert.IsInstanceOfType(res.ViewData.Model, typeof(HomepageViewModel));
        }

        [TestMethod]
        public void Index_Loads_ModelsAndNews()
        {
            // Arrange
            HomeController controller = this.GetController();
            mocks.ReplayAll();

            // Act
            controller.Index();

            // Assert
            mocks.VerifyAll();
        }
        
    }
}
